<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>30 购物车</title>
    <script src="./js/vue.js"></script>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        .table {
            width: 1200px;
            margin: 50px auto;
        }

        .tr {
            width: 1200px;
            margin: 0 auto;
        }

        .tr .item {
            width: 200px;
            height: 80px;
            line-height: 80px;
            float: left;
            background-color: midnightblue;
            color: orange;
            text-align: center;
        }

        .table .tr .item input {
            width: 80px;
            text-align: center;
        }

        .table .tr .item button {
            width: 40px;
        }
    </style>
</head>

<body>
    <div id="app">
        <myheader :checkedall="checkedAll" @setcheckedall="setcheckedAll"></myheader>
        <mylist :list="list" @dellistbyid="delListById"></mylist>
        <myfooter :totalnum="totalnum"  :totalprice="totalprice" @alldel="Alldel"></myfooter>
    </div>
</body>
<!-- 头部 -->
<template id="myheader">
    <div class="tr">
        <div class="item">
            全选
            <input type="checkbox" :checked="checkedall" @change="setcheckedAll">
        </div>
        <div class="item">
            商品名称
        </div>
        <div class="item">
            商品价格
        </div>
        <div class="item">
            数量
        </div>
        <div class="item">
            小计
        </div>
        <div class="item">
            操作
        </div>
    </div>
</template>

<!-- 列表 -->
<template id="mylist">
    <div class="table">
        <div class="tr" v-for="item in list" :key="item.id">
            <div class="item">
                <input type="checkbox" v-model="item.ischecked">
            </div>
            <div class="item">
                {{item.goodsname}}
            </div>
            <div class="item">
                {{item.goodsprice}}
            </div>
            <div class="item">
                <button v-show="item.goodsnum > 1" @click="calc(item,-1)">-</button>
                <input type="number" v-model="item.goodsnum">
                <button @click="calc(item,1)">+</button>
            </div>
            <div class="item">
                {{(item.goodsnum * item.goodsprice).toFixed(2)}}
            </div>
            <div class="item" @click="del(item.id)">
                删除
            </div>
        </div>
    </div>
</template>

<!-- 尾部 -->
<template id="myfooter" >
    <div class="tr">
        <div class="item" @click="Alldel">
            全删
        </div>
        <div class="item">
            
        </div>
        <div class="item">
            
        </div>
        <div class="item">
            总数
            {{totalnum}}
        </div>
        <div class="item">
            总价
            {{(totalprice).toFixed(2)}}
        </div>
        <div class="item">
        
        </div>
    </div>
</template>



<script>

    // 列表
    let mylist = {
        props: ["list"],
        template: "#mylist",
        methods: {
            calc(item, num) {
                item.goodsnum = item.goodsnum + num;
            },
            del(id) {
                this.$emit("dellistbyid", id)
            }
        }
    };

    // 头部
    let myheader = {
        template: "#myheader",
        props:["checkedall"],
        methods:{
            setcheckedAll(){
                let flag = window.event.target.checked;

                this.$emit("setcheckedall",flag)
            }
        }

    };

    // 尾部
    let myfooter = {
        template:"#myfooter",
        props:["totalnum","totalprice"],
        methods:{
            Alldel(){
                this.$emit("alldel")
            }
        }
    }


    new Vue({
        el: "#app",
        data: {
            checkedAll:false,
            // totalnum:0,
            // totalprice:0.00,
            list: [
                { id: 1, goodsname: "商品一", goodsnum: 10, goodsprice: 100, ischecked: false },
                { id: 2, goodsname: "商品二", goodsnum: 10, goodsprice: 100, ischecked: false },
                { id: 3, goodsname: "商品三", goodsnum: 10, goodsprice: 100, ischecked: false },
            ],

        },
        methods: {
            delListById(id) {
                this.list = this.list.filter(item => item.id != id);
            },
            setcheckedAll(flag){
                this.list.forEach(item => item.ischecked = flag);
            },
            Alldel(){
                this.list = this.list.filter(item => item.ischecked == false);
            }
            

        },
        watch: {
            list:{
                deep:true,
                handler(){
                    if(this.list.length){
                        this.checkedAll = this.list.every(item => item.ischecked);
                    }else{
                        this.checkedAll = false;
                    }    
                }
            }

        },
        computed:{
            totalnum(){
                let selectList = this.list.filter(item => item.ischecked == true);
                if(selectList.length){
                    return selectList.map(item =>item.goodsnum).reduce((a,b) => a+b);
                }else{
                    return 0;
                }
            },
            totalprice(){
                let selectList = this.list.filter(item => item.ischecked == true);
                if(selectList.length){
                    return selectList.map(item => item.goodsnum*item.goodsprice).reduce((a,b) => a+b); 
                }else{
                    return 0.00;
                }
            }
        },
        components: {
            mylist,
            myheader,
            myfooter,
        }
    })
</script>

</html>